<?php

namespace app\common\model\proxy;

use app\common\model\BaseModel;

class Region extends BaseModel
{
    protected $name = 'proxy_region';
    protected $pk = 'proxy_region_id';

    public static function checkExist($region_id, $exclude_proxy_id=0)
    {
        return !!static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("region_id", '=', $region_id)
            ->where("proxy_id", '<>', $exclude_proxy_id)
            ->value('proxy_region_id');
    }

    public static function getRegions($proxy_id)
    {
        $list = static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $proxy_id)
            ->field(['proxy_region_id', 'province_id', 'city_id', 'county_id'])
            ->select();
        $regions = [];
        foreach ($list as $item) {
            $regions[] = array_filter([$item["province_id"], $item["city_id"], $item['county_id']], function ($item) {
                return $item && $item > 0;
            });
        }
        return $regions;
    }

    /**
     * 关联应用表
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'proxy_id', 'proxy_id');
    }


}